﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Visio.ForwardEngineer.DataModel
{
    /// <summary>
    /// Holds the complete data model
    /// </summary>
    public class ModelStructure
    {
        public TableList Tables;        // List of the tables in the model

        /// <summary>
        /// Standard constructor for the data model
        /// </summary>
        public ModelStructure()
        {
            Tables = new TableList();
        }

        /// <summary>
        /// Generates the CREATE DATABASE Statement
        /// </summary>
        /// <param name="databaseName"></param>
        /// <returns></returns>
        public string ScriptCreateDatabase(string databaseName)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("USE Master");
            result.AppendLine("GO");
            result.AppendLine(String.Format("CREATE DATABASE [{0}]", databaseName));
            result.AppendLine("GO");
            result.AppendLine(String.Format("USE [{0}]", databaseName));
            result.AppendLine("GO");

            return result.ToString();
        }

        /// <summary>
        /// Generates the Drop DATABASE Statement
        /// </summary>
        /// <param name="databaseName"></param>
        /// <returns></returns>
        public string ScriptDropDatabase(string databaseName)
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine(String.Format("EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'{0}'", databaseName));
            result.AppendLine("GO");
            result.AppendLine("USE [master]");
            result.AppendLine("GO");
            result.AppendLine(String.Format("ALTER DATABASE [{0}] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE", databaseName));
            result.AppendLine("GO");
            result.AppendLine("USE [master]");
            result.AppendLine("GO");
            result.AppendLine(String.Format("DROP DATABASE [{0}]", databaseName));
            result.AppendLine("GO");
            return result.ToString();
        }

        public string ScriptUseTransactionsStart()
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("BEGIN TRAN");
            result.AppendLine("GO");
            return result.ToString();
        }

        public string ScriptUseTransactionsEnd()
        {
            StringBuilder result = new StringBuilder();

            result.AppendLine("COMMIT TRAN");
            result.AppendLine("GO");
            return result.ToString();
        }
    }
}
